import Vue from 'vue'
import Store from '../store'
import request from '@/utils/request'
import Router from 'vue-router'
import local from '@/utils/localstorage'
import { Toast, Indicator } from 'mint-ui'
import { userInfo } from '@/api'

import Message from '@/views/message'
import MessageStu from '@/views/messageStu'
import MessageTeacher from '@/views/messageTeacher'
import Notice from '@/views/notice'

//  学生端
import Login from '@/views/login'
import Index from '@/views/student/index'
import UserInfo from '@/views/student/userInfo'
import Training from '@/views/student/training'
import Opening from '@/views/student/opening'
import Research from '@/views/student/research'
import MidExam from '@/views/student/midExam'
import Reply from '@/views/student/reply'
import StudentCourse from '@/views/student/studentCourse'
import Achievement from '@/views/student/achievement'
import Paper from '@/views/student/paper'

// 教师端
import TeacherIndex from '@/views/teacher/index'
import TeacherUserInfo from '@/views/teacher/userInfo'
import TeacherTraining from '@/views/teacher/training'
import TeacherTrainingDetail from '@/views/teacher/training/detail'
import TeacherCourse from '@/views/teacher/teacherCourse'
import TeacherAchievement from '@/views/teacher/achievement'
import TeacherOpening from '@/views/teacher/opening'
import TeacherResearch from '@/views/teacher/research'
import TeacherMidExam from '@/views/teacher/midExam'
import TeacherReply from '@/views/teacher/reply'
import TeacherPaper from '@/views/teacher/paper'

// 管理端
import ManageIndex from '@/views/manage/index'
import ManageTraining from '@/views/manage/training'
import ManageTrainingDetail from '@/views/manage/training/detail'
import ManageCourse from '@/views/manage/teacherCourse'
import ManageAchievement from '@/views/manage/achievement'
import ManageOpening from '@/views/manage/opening'
import ManageResearch from '@/views/manage/research'
import ManageMidExam from '@/views/manage/midExam'
import ManageReply from '@/views/manage/reply'
import ManagePaper from '@/views/manage/paper'
import ManageStuInfo from '@/views/manage/userInfo/stu' //学生信息查询
import ManageStuInfoDetail from '@/views/manage/userInfo/stuDetail' //学生信息查询

import ManageTeacherInfo from '@/views/manage/userInfo/teacher' //学生信息查询
import ManageTeacherInfoDetail from '@/views/manage/userInfo/teacherDetail' //学生信息查询

Vue.use(Router)

const RouterList = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'index',
    component: Index,
    meta: {
      title: '首页',
      isLogin: true
    }
  },
  {
    path: '/index/userInfo',
    name: 'userInfo',
    component: UserInfo,
    meta: {
      title: '研究生学籍信息',
      isLogin: true
    }
  },
  {
    path: '/index/training', // 培养计划
    name: 'training',
    component: Training,
    meta: {
      title: '培养计划',
      isLogin: true
    }
  },
  {
    path: '/index/opening', // 开题报告
    name: 'opening',
    component: Opening,
    meta: {
      title: '开题报告',
      isLogin: true
    }
  },

  {
    path: '/index/research', // 科研成果信息管理
    name: 'research',
    component: Research,
    meta: {
      title: '科研成果信息管理',
      isLogin: true
    }
  },
  {
    path: '/index/midexam', // 中期考核
    name: 'midExam',
    component: MidExam,
    meta: {
      title: '中期检查信息',
      isLogin: true
    }
  },
  {
    path: '/index/reply', // 预答辩
    name: 'reply',
    component: Reply,
    meta: {
      title: '预答辩信息',
      isLogin: true
    }
  },
  {
    path: '/index/studentCourse', // 课程表
    name: 'studentCourse',
    component: StudentCourse,
    meta: {
      title: '课程表',
      isLogin: true
    }
  },
  {
    path: '/index/achievement', // 学生总成绩单
    name: 'achievement',
    component: Achievement,
    meta: {
      title: '学生总成绩单',
      isLogin: true
    }
  },
  {
    path: '/index/paper', // 学生总成绩单
    name: 'paper',
    component: Paper,
    meta: {
      title: '学生总成绩单',
      isLogin: true
    }
  },
  // -------------------------------------------   导师    ---------------------------------------
  {
    path: '/teacher', // 学生总成绩单
    name: 'TeacherIndex',
    component: TeacherIndex,
    meta: {
      title: '教师端首页',
      isLogin: true
    }
  },
  {
    path: '/teacher/userInfo',
    name: 'TeacherUserInfo',
    component: TeacherUserInfo,
    meta: {
      title: '研究生学籍信息',
      isLogin: true
    }
  },
  {
    path: '/teacher/training', // 培养计划
    name: 'TeacherTraining',
    component: TeacherTraining,
    meta: {
      title: '培养计划',
      isLogin: true
    }
  },
  {
    path: '/teacher/training/detail', // 培养计划
    name: 'TeacherTrainingDetail',
    component: TeacherTrainingDetail,
    meta: {
      title: '培养计划学生详情',
      isLogin: true
    }
  },
  {
    path: '/teacher/achievement', // 培养计划
    name: 'TeacherAchievement',
    component: TeacherAchievement,
    meta: {
      title: '学生总成绩单',
      isLogin: true
    }
  },
  {
    path: '/teacher/opening', // 开题报告
    name: 'TeacherOpening',
    component: TeacherOpening,
    meta: {
      title: '开题报告',
      isLogin: true
    }
  },
  {
    path: '/teacher/research', // 科研成果信息管理
    name: 'TeacherResearch',
    component: TeacherResearch,
    meta: {
      title: '科研成果信息管理',
      isLogin: true
    }
  },
  {
    path: '/teacher/midexam', // 中期考核
    name: 'TeacherMidExam',
    component: TeacherMidExam,
    meta: {
      title: '中期检查信息',
      isLogin: true
    }
  },
  {
    path: '/teacher/reply', // 预答辩
    name: 'TeacherReply',
    component: TeacherReply,
    meta: {
      title: '预答辩信息',
      isLogin: true
    }
  },
  {
    path: '/teacher/teacherCourse', // 课程表
    name: 'TeacherCourse',
    component: TeacherCourse,
    meta: {
      title: '课程表',
      isLogin: true
    }
  },
  {
    path: '/teacher/paper', // 学生总成绩单
    name: 'TeacherPaper',
    component: TeacherPaper,
    meta: {
      title: '学生总成绩单',
      isLogin: true
    }
  },

  // -------------------------------------------  管理端    ---------------------------------------
  {
    path: '/manage', // 管理端
    name: 'ManageIndex',
    component: ManageIndex,
    meta: {
      title: '教师端首页',
      isLogin: true
    }
  },
  // {
  //   path: '/manage/userInfo',
  //   name: 'ManageUserInfo',
  //   component: ManageUserInfo,
  //   meta: {
  //     title: '研究生学籍信息',
  //     isLogin: true
  //   }
  // },
  {
    path: '/manage/training', // 培养计划
    name: 'ManageTraining',
    component: ManageTraining,
    meta: {
      title: '培养计划',
      isLogin: true
    }
  },
  {
    path: '/manage/training/detail', // 培养计划
    name: 'ManageTrainingDetail',
    component: ManageTrainingDetail,
    meta: {
      title: '培养计划学生详情',
      isLogin: true
    }
  },
  {
    path: '/manage/achievement', // 培养计划
    name: 'ManageAchievement',
    component: ManageAchievement,
    meta: {
      title: '学生总成绩单',
      isLogin: true
    }
  },
  {
    path: '/manage/opening', // 开题报告
    name: 'ManageOpening',
    component: ManageOpening,
    meta: {
      title: '开题报告',
      isLogin: true
    }
  },
  {
    path: '/manage/research', // 科研成果信息管理
    name: 'ManageResearch',
    component: ManageResearch,
    meta: {
      title: '科研成果信息管理',
      isLogin: true
    }
  },
  {
    path: '/manage/midexam', // 中期考核
    name: 'ManageMidExam',
    component: ManageMidExam,
    meta: {
      title: '中期检查信息',
      isLogin: true
    }
  },
  {
    path: '/manage/reply', // 预答辩
    name: 'ManageReply',
    component: ManageReply,
    meta: {
      title: '预答辩信息',
      isLogin: true
    }
  },
  {
    path: '/manage/teacherCourse', // 课程表
    name: 'ManageCourse',
    component: ManageCourse,
    meta: {
      title: '课程表',
      isLogin: true
    }
  },
  {
    path: '/manage/paper', //
    name: 'ManagePaper',
    component: ManagePaper,
    meta: {
      title: '学生论文',
      isLogin: true
    }
  },
  {
    path: '/manage/stuInfo', // ManageStuInfoDetail
    name: 'ManageStuInfo',
    component: ManageStuInfo,
    meta: {
      title: '学生信息查询',
      isLogin: true
    }
  },
  {
    path: '/manage/stuInfo/detail', // ManageStuInfoDetail
    name: 'ManageStuInfoDetail',
    component: ManageStuInfoDetail,
    meta: {
      title: '学生信息查询',
      isLogin: true
    }
  },
  {
    path: '/manage/teacherInfo', // ManageStuInfoDetail
    name: 'ManageTeacherInfo',
    component: ManageTeacherInfo,
    meta: {
      title: '教师信息查询',
      isLogin: true
    }
  },
  {
    path: '/manage/teacherInfo/detail', // ManageStuInfoDetail Message
    name: 'ManageTeacherInfoDetail',
    component: ManageTeacherInfoDetail,
    meta: {
      title: '学生信息查询',
      isLogin: true
    }
  },
  {
    path: '/message', // ManageStuInfoDetail Notice
    name: 'Message',
    component: Message,
    meta: {
      title: '消息中心',
      isLogin: true
    }
  },
  {
    path: '/messageStu', // ManageStuInfoDetail Notice
    name: 'MessageStu',
    component: MessageStu,
    meta: {
      title: '消息中心',
      isLogin: true
    }
  },
  {
    path: '/messageTeacher', // ManageStuInfoDetail Notice
    name: 'MessageTeacher',
    component: MessageTeacher,
    meta: {
      title: '消息中心',
      isLogin: true
    }
  },

  {
    path: '/notice', // ManageStuInfoDetail Notice
    name: 'Notice',
    component: Notice,
    meta: {
      title: '消息中心',
      isLogin: true
    }
  }
]

// 单点登录处理
const getUserInfo = (token, userType, callback) => {
  if (!userType) {
    Toast({
      message: '缺少登录类型参数',
      duration: 2000
    })
    return
  }
  Indicator.open()
  request
    .get(`${userInfo}/${token}`, {})
    .then((res) => {
      const r = res.data
      Indicator.close()
      let url = ''
      if (r && r.data && r.status === 1) {
        Store.commit('account/setToken', r.data.token)
        Store.commit('account/setUser', r.data.user)

        if (userType === 'index') {
          url = '/'
          Store.commit('account/userType', 'STUDENT')
        }
        if (userType === 'TeacherIndex') {
          url = '/teacher'
          Store.commit('account/userType', 'TEACHER')
        }
        if (userType === 'ManageIndex') {
          url = '/teacher'
          Store.commit('account/userType', 'SYSTEM')
        }
        callback && callback(url)
      } else {
        Toast({
          message: r.msg || '登录失败,请重试',
          duration: 2000
        })
      }
    })
    .catch((err) => {
      Toast({
        message: '登录失败,请重试',
        duration: 2000
      })
    })
}

const router = new Router({
  routes: RouterList
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // console.log('to', to)
  // console.log('from', from)

  //单点登录 处理
  if (to.query && to.query.token) {
    local.clear()
    getUserInfo(to.query.token, to.name, function(url) {
      next({ path: url })
    })
  }

  if (
    local.get('USER_TOKEN') &&
    to.name === 'Login' &&
    local.get('USER_TYPE') &&
    local.get('USER_TYPE') === 'STUDENT'
  ) {
    next({ path: '/' })
  } else if (
    local.get('USER_TOKEN') &&
    to.name === 'Login' &&
    local.get('USER_TYPE') &&
    local.get('USER_TYPE') === 'TEACHER'
  ) {
    next({ path: '/teacher' })
  } else if (
    local.get('USER_TOKEN') &&
    to.name === 'Login' &&
    local.get('USER_TYPE') &&
    local.get('USER_TYPE') === 'SYSTEM'
  ) {
    next({ path: '/manage' })
  } else if (to.matched.some((res) => res.meta.isLogin)) {
    if (local.get('USER_TOKEN')) {
      if (to.name === 'index' && local.get('USER_TYPE') === 'TEACHER') {
        next({ path: '/teacher' })
      } else if (to.name === 'index' && local.get('USER_TYPE') === 'SYSTEM') {
        next({ path: '/manage' })
      } else {
        next()
      }
    } else {
      next({
        path: '/login',
        query: {}
      })
    }
  } else {
    next()
  }
})
export default router
